<template>
  <div class="about">
    <!-- 用来放置后台返回的html页面 -->
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>会议管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索与申请会议区 -->
      <el-form :inline="true">
        <el-form-item>
          <el-date-picker
            placeholder="请输入查询日期"
            v-model="info.createTime"
            @click.native="getUserList"
            clearable
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" @click="getUserList" round
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 会议申请，加入等按钮 -->
      <el-row>
        <el-col :span="4"
          ><el-button type="primary" @click="addDialogVisible = true"
            >会议申请</el-button
          ></el-col
        >
        <el-col :span="4"
          ><el-button
            type="success"
            @click="joinDialogVisible = true"
            :disabled="false"
            >加入会议</el-button
          ></el-col
        >
        <el-col :span="4"
          ><el-button type="primary" @click="launchDialogVisible = true"
            >发起会议</el-button
          ></el-col
        >
        <!-- <el-col :span="4"
          ><el-button type="success">
            <div>
              <el-link
                href="http://172.27.132.27:8080/meetingIndex.html"
                target="_blank"
                >会议入口</el-link
              >
            </div>
          </el-button></el-col
        > -->
      </el-row>
      <!-- 会议列表区 -->
      <el-table border stripe :data="userList">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="会议主题" prop="title"> </el-table-column>
        <el-table-column label="会议号" prop="roomId"> </el-table-column>
        <el-table-column label="创建者" prop="createBy"> </el-table-column>
        <el-table-column label="预约时间" prop="createTime"> </el-table-column>
        <el-table-column label="开始时间" prop="startTime"> </el-table-column>
        <el-table-column label="状态" prop="state"> </el-table-column>
        <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="removeId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.currentPage"
        :page-sizes="[5, 10, 20, 100]"
        :page-size="info.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 申请会议信息 -->
      <el-dialog
        title="申请会议"
        :visible.sync="addDialogVisible"
        width="28%"
        @close="addDialogClosed"
      >
        <!-- 内容主题区 -->
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item label="会议主题" prop="title">
            <el-input v-model="addForm.title"></el-input>
          </el-form-item>
          <el-form-item label="创建者" prop="createBy">
            <el-input v-model="addForm.createBy"></el-input>
          </el-form-item>
          <el-form-item label="开始时间" prop="startTime">
            <el-date-picker
              v-model="addForm.startTime"
              type="datetime"
            ></el-date-picker>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addMeet">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 加入会议 -->
      <el-dialog
        title="加入会议"
        :visible.sync="joinDialogVisible"
        width="35%"
        @close="joinDialogClosed"
      >
        <!-- 内容主题区 -->
        <el-form
          :model="joinForm"
          :rules="joinFormRules"
          ref="joinFormRef"
          label-width="100px"
        >
          <el-form-item label="会议号" prop="roomId">
            <el-input v-model="joinForm.roomId"></el-input>
          </el-form-item>
          <el-form-item label="入会名称" prop="name">
            <el-input v-model="joinForm.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="joinMeet" :loading="loading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
      <!-- 发起会议 -->
      <el-dialog
        title="发起会议"
        :visible.sync="launchDialogVisible"
        width="35%"
        @close="launchDialogClosed"
      >
        <!-- 内容主题区 -->
        <el-form
          :model="launchForm"
          :rules="launchFormRules"
          ref="launchFormRef"
          label-width="100px"
        >
          <el-form-item label="会议主题" prop="title">
            <el-input v-model="launchForm.title"></el-input>
          </el-form-item>
          <el-form-item label="创建者" prop="createBy">
            <el-input v-model="launchForm.createBy"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel1">取 消</el-button>
          <el-button type="primary" @click="launchMeet" :loading="loading1">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //加载
      loading: false,
      loading1: false,
      //获取会议信息及搜索的请求参数
      info: {
        createTime: "",
        currentPage: 1,
        size: 4,
      },
      userList: [],
      total: 0,
      // 申请会议对话框隐藏
      addDialogVisible: false,
      //申请会议的表单数据
      addForm: {
        title: "",
        createBy: "",
        startTime: "",
        // endTime: "",
      },
      // 加入会议对话框隐藏
      joinDialogVisible: false,
      joinForm: {
        roomId: "",
        name: "",
      },
      launchDialogVisible: false,
      launchForm: {
        createBy: "",
        title: "",
      },
      //申请会议的规则验证对象
      addFormRules: {
        title: [
          { required: true, message: "请输入会议主题", trigger: "blur" },
          {
            min: 2,
            max: 200,
            message: "部门名称为2~200个字符之间",
            trigger: "blur",
          },
        ],
        createBy: [
          { required: true, message: "请输入创建者", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "备注名称为2~50个字符之间",
            trigger: "blur",
          },
        ],
        // startTime: [
        //   { required: true, message: "请输入开始时间", trigger: "blur" },
        //   {
        //     min: 2,
        //     max: 50,
        //     message: "简介名称为2~50个字符之间",
        //     trigger: "blur",
        //   },
        // ],
      },
      joinFormRules: {
        roomId: [
          { required: true, message: "请输入会议号", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "会议号为2~20个字符之间",
            trigger: "blur",
          },
        ],
        name: [
          { required: true, message: "请输入入会名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "入会名称为2~20个字符之间",
            trigger: "blur",
          },
        ],
      },
      launchFormRules: {
        title: [
          { required: true, message: "请输入会议主题", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "会议主题为2~200个字符之间",
            trigger: "blur",
          },
        ],
        createBy: [
          { required: true, message: "请输入会议创建者", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "会议创建者为2~20个字符之间",
            trigger: "blur",
          },
        ],
      },
      meet: {},
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    //获取会议信息
    async getUserList() {
      const { data: res } = await this.$http.get("meeting/queryMeeting", {
        params: this.info,
      });
      if (res.flag !== true) {
        return this.$message.error("获取会议信息失败!");
      }
      this.userList = res.rows;
      this.total = res.total;
      console.log(res);
    },
    //监听信息条数
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.info.size = newSize;
      this.getUserList();
    },
    //监听页数
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.info.currentPage = newPage;
      this.getUserList();
    },
    //监听申请会议表单的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //点击按钮申请会议
    addMeet() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起申请会议的网络请求
        const { data: res } = await this.$http.get("meeting/subscribeMeeting", {
          params: this.addForm,
        });
        console.log(res);
        this.meet = res.data.roomId;
        if (res.flag !== true) {
          return this.$message.error("会议预约失败!");
        }
        this.$message.success("会议预约成功!你预约的会议号是" + this.meet);
        //隐藏申请会议信息的对话框
        this.addDialogVisible = false;
        //重新获取申请会议列表数据
        this.getUserList();
      });
    },
    //点击按钮删除会议信息
    async removeId(id) {
      // console.log(`删除数据的id = ${id}`)

      //弹框询问用户是否删除部门const confirmResult = await
      const confirmResult = await this.$confirm(
        "此操作将永久删除该会议, 是否继续?",
        "删除会议",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      //如果确认删除部门信息，那返回的信息就是字符串confirm
      //如果取消删除部门信息，那返回的信息就是字符串cancel
      // console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await this.$http.post("meeting/deleteMeeting/", {
        id: id,
      });
      console.log(res);

      if (res.flag !== true) {
        return this.$message.error("删除会议信息失败");
      }
      this.$message.success("删除会议信息成功");
      this.getUserList();
    },
    //监听加入会议表单的关闭事件
    joinDialogClosed() {
      this.$refs.joinFormRef.resetFields();
    },
    //加入会议取消按钮操作
    cancel() {
      this.joinDialogVisible = false;
      this.loading = false;
    },
    cancel1() {
      this.launchDialogVisible = false;
      this.loading1 = false;
    },
    //加入会议
    joinMeet() {
      this.loading = true;
      this.$refs.joinFormRef.validate(async (valid) => {
        if (!valid) return;
        this.$http
          .get("meeting/joinMeeting", {
            params: this.joinForm,
          })
          .then((res) => {
            console.log(res);
            // res.data 为接口返回的html完整文件代码
            // 必须要存进localstorage，否则会报错，显示不完全
            window.localStorage.removeItem("callbackHTML");
            window.localStorage.setItem("callbackHTML", res.data);
            // 读取本地保存的html数据，使用新窗口打开
            var newWin = window.open("", "_blank");
            newWin.document.write(localStorage.getItem("callbackHTML"));
            // 关闭输出流
            newWin.document.close();
            if (res.status !== 200) {
              return this.$message.error("加入会议失败!");
            }
            this.$message.success("加入会议成功!");
            //隐藏申请会议信息的对话框
            this.joinDialogVisible = false;
            this.loading = false;
            //重新获取申请会议列表数据
            this.getUserList();
          });
      });
    },
    //监听发起会议表单的关闭事件
    launchDialogClosed() {
      this.$refs.launchFormRef.resetFields();
    },
    //发起会议
    launchMeet() {
      this.loading1 = true;
      this.$refs.launchFormRef.validate(async (valid) => {
        if (!valid) return;
        this.$http
          .get("meeting/launchMeeting", {
            params: this.launchForm,
          })
          .then((res) => {
            console.log(res);
            // res.data 为接口返回的html完整文件代码
            // 必须要存进localstorage，否则会报错，显示不完全
            window.localStorage.removeItem("callbackHTML");
            window.localStorage.setItem("callbackHTML", res.data);
            // 读取本地保存的html数据，使用新窗口打开
            var newWin = window.open("", "_blank");
            newWin.document.write(localStorage.getItem("callbackHTML"));
            // 关闭输出流
            newWin.document.close();
            if (res.status !== 200) {
              return this.$message.error("发起会议失败!");
            }
            this.$message.success("发起会议成功!");
            //隐藏申请会议信息的对话框
            this.launchDialogVisible = false;
            this.loading1 = false;
            //重新获取申请会议列表数据
            this.getUserList();
          });
      });
    },
  },
};
</script>

<style></style>
